//
//  ListTabItem2.swift
//  SwiftUI+MVVM+Repository
//
//  Created by 高强 on 2024/9/18.
//

import SwiftUI

//第二个tab样式
struct ListTabItem: View {
    
    // 使用 @State 使 items 可变
    @State var items = [
        ListItemBean(title: "标题1", content: "内容1"),
        ListItemBean(title: "标题2", content: "内容2"),
        ListItemBean(title: "标题3", content: "内容3"),
    ]
    
    var body: some View {
        List(items) { item in
            HStack {
                Text(item.title)
                    .font(.body)
                Text(item.content)
                    .font(.headline)
            }
            .padding()
            // 上拉加载，当列表显示到最后一个item时，触发加载更多数据
            .onAppear{
                if let lastitem  = items.last,item == lastitem {
                    print("上拉加载")
                    
                    let items2 = [
                        ListItemBean(title: "标题21", content: "内容21"),
                        ListItemBean(title: "标题22", content: "内容22"),
                        ListItemBean(title: "标题23", content: "内容23")
                    ]
                    
                    items.append(contentsOf: items2)
                }
            }
        }
        .refreshable {
            print("下拉刷新")
            let items1 = [
                ListItemBean(title: "标题10", content: "内容10"),
                ListItemBean(title: "标题11", content: "内容11"),
                ListItemBean(title: "标题12", content: "内容12"),
                ListItemBean(title: "标题13", content: "内容13"),
                ListItemBean(title: "标题14", content: "内容14"),
            ]
            
            items = items1
            
        }
        
    }
}

#if DEBUG
struct ListTabItem2_Previews : PreviewProvider{
    static var previews: some View{
        ListTabItem()
    }
}
#endif


